<?php

class Application_Model_Order
{
    /**
     * @param string|Zend_Date $from
     * @param int $userID
     * @return int
     */
    public static function getNumberOfProcessedOrders($from, $userID)
    {
        $dbAdapter = JDbAdapter::factory();

        if ($from instanceof Zend_Date) {
            $from = $from->get('y-M-d H:m:s');
        }

        $numberOfProcessedOrders = $dbAdapter->fetchOne(
            $dbAdapter->select()
                ->from('ORDERS', 'COUNT(ID)')
                ->where('CreatedDate > ?', $from)
                ->where('UserID = ?', $userID)
        );

        return $numberOfProcessedOrders;
    }

    /**
     * @param string $name
     * @param int $totalCost
     * @param int $userID
     * @return int
     */
    public static function addOrder($name, $totalCost, $userID)
    {
        $dbAdapter = JDbAdapter::factory();

        try {
            $dbAdapter->insert(
                'ORDERS',
                array(
                    'Name' => $name,
                    'TotalCost' => $totalCost,
                    'UserID' => $userID
                )
            );

            return (int)$dbAdapter->lastInsertId('ORDERS');
        } catch (Exception $ex) {
            JLog::log($ex->getMessage());
            return 0;
        }
    }

    /**
     * @param int[] $ids
     * @return bool
     */
    public static function deleteOrders($ids)
    {
        $dbAdapter = JDbAdapter::factory();

        try {
            $str = '(';
            $length = count($ids);
            for ($i = 0; $i < $length - 1; $i++) {
                $str .= $ids[$i] . ', ';
            }
            $str .= $ids[$length - 1];
            $str .= ')';
            $numberOfAffects = $dbAdapter->update(
                'ORDERS',
                array('Deleted' => true), "ID IN $str"
            );
            if ($numberOfAffects == 0) {
                return false;
            }

            return true;
        } catch (Exception $ex) {
            JLog::log($ex->getMessage());
            return false;
        }
    }

    /**
     * @param int $id
     * @return bool
     */
    public static function confirmOrder($id)
    {
        $dbAdapter = JDbAdapter::factory();

        try {
            $numberOfAffects = $dbAdapter->update(
                'ORDERS', array('IsProcess' => true), "ID = $id"
            );
            if ($numberOfAffects == 0) {
                return false;
            }

            return true;
        } catch (Exception $ex) {
            JLog::log($ex->getMessage());
            return false;
        }
    }

    /**
     * @param int $id
     * @return bool
     */
    public static function unconfirmOrder($id)
    {
        $dbAdapter = JDbAdapter::factory();

        try {
            $numberOfAffects = $dbAdapter->update(
                'ORDERS', array('IsProcess' => false), "ID = $id"
            );
            if ($numberOfAffects == 0) {
                return false;
            }

            return true;
        } catch (Exception $ex) {
            JLog::log($ex->getMessage());
            return false;
        }
    }
}
